<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="{row}">
                  <el-button size="small" type="success" @click="hAssign(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              :current-page="q.page"
              :page-sizes="[2,3,5,10]"
              :page-size="q.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />

          </el-tab-pane>
          <!-- 新增弹框 -->
          <el-dialog
            :title="isEdit? '编辑' :'添加'"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :visible.sync="showDialog"
            @closed="hClosed"
          >
            <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="roleForm.name" />
              </el-form-item>
              <el-form-item label="角色描述">
                <el-input v-model="roleForm.description" />
              </el-form-item>
            </el-form>
            <!-- 底部 -->
            <el-row slot="footer" type="flex" justify="center">
              <el-col :span="6">
                <el-button size="small" @click="hClose">取消</el-button>
                <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
              </el-col>
            </el-row>
          </el-dialog>
          <el-dialog
            v-if="showDialogAssign"
            title="分配权限"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :visible.sync="showDialogAssign"
          >
            <assign-permission :id="curId" @close="showDialogAssign = false" />
          </el-dialog>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>
<script>
import assignPermission from './assignPermission.vue'
import { addRole, getRoles, updateRole } from '@/api/setting'
import { deleteRoleById } from '@/api/setting'
export default {
  components: {
    assignPermission
  },
  data() {
    return {
      q: {
        page: 1,
        pagesize: 2
      },
      roles: [],
      total: 0,
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      // 是否是编辑状态
      isEdit: '',
      showDialogAssign: false,
      curId: ''
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async loadRoles() {
      try {
        const res = await getRoles(this.q)
        // console.log(res)
        this.roles = res.data.rows
        // console.log(this.roles)
        this.total = res.data.total
      } catch (e) {
        console.log(e)
      }
    },
    handleSizeChange(pagesize) {
      this.q.pagesize = pagesize
      this.loadRoles()
    },
    handleCurrentChange(page) {
      this.q.page = page
      this.loadRoles()
    },
    hDel(id) {
      // 根据提示删除
      this.$confirm('删除吗', '提示', {
        type: 'warning'
      }).then(async() => {
        try {
          if (this.roles.length === 1 && this.q.page > 1) {
            this.q.page--
          }
          // 发送请求删除数据，根据id
          const res = await deleteRoleById(id)
          // 根据结果提醒用户
          this.$message.success(res.message)
          // 重新渲染
          this.loadRoles()
        } catch (e) {
          this.$message.error(e.message)
        }
      }
      )
        .catch(() => {})
    },
    hSubmit() {
      // 兜底校验
      this.$refs.roleForm.validate(valid => {
        if (!valid) return
        // console.log(111)
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    async doAdd() {
      try {
        // 发请求
        const res = await addRole(this.roleForm)
        // 成功提示
        this.$message.success(res.message)
        // 关闭弹层
        this.showDialog = false
        // 如果最后一页已经满了，将total总条数+1
        if (this.total % this.q.pagesize === 0) {
          this.total++
        }
        // 调到最后一页
        this.q.page = Math.ceil(this.total / this.q.pagesize)

        // 表单清空
        this.roleForm = ''
        // 再次渲染
        this.loadRoles()

        // 失败的提示
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    async doEdit() {
      try {
        // 发请求
        const res = await updateRole(this.roleForm)
        // 成功提示
        this.$message.success(res.message)
        // 关闭弹层
        this.showDialog = false
        // 如果最后一页已经满了，将total总条数+1

        // 表单清空
        this.roleForm = ''
        // 再次渲染
        this.loadRoles()

        // 失败的提示
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 点击取消关闭弹层
    hClose() {
      this.showDialog = false
    },
    hClosed() {
      // 清空表单
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除表单项的校验结果
      this.$refs.roleForm.clearValidate()
    },
    hEdit(row) {
      this.isEdit = true
      console.log(row)
      this.showDialog = true
      this.roleForm = { ...row }
    },
    hAdd() {
      this.isEdit = false
      // 显示 dialog
      this.showDialog = true
    },
    hAssign(id) {
      this.showDialogAssign = true
      this.curId = id
    }
  }

}
</script>
<style >

